<template>
    <el-row class="login-container">
    <el-col :xs="24" :sm="24" :md="12" :lg="16" :xl="16" class="left">
      <div>
        <h1 class="font-bold text-5xl mb-5">后台管理系统</h1>
        <p class="text-xl mt-4" style="display: inline">
          <span
            ><img
              src="../assets/vue.svg"
              class="w-10"
              style="display: inline"
            />
            (Vue3) +
            <img
              src="../assets/elementpuls.svg"
              class="w-30"
              style="display: inline"
            />
            (ElementPuls) +
            <img
              src="../assets/vite.svg"
              class="w-10"
              style="display: inline"
            />
            (Vite) 构建的后台商城管理系统</span
          >
        </p>
      </div>
    </el-col>
    <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8" class="right">
      <h2 class="font-bold text-4xl text-gray-600 mb-5">登 录</h2>
      <el-divider class="w-100"
        ><span class="text-lg text-gray-400 font-bold bg-transparent">
          后台登录</span
        ></el-divider
      >
      <el-form ref="formRef" :rules="rules" :model="form" class="w-70 mt-5">
        <el-form-item prop="username">
          <el-input
            :prefix-icon="User"
            size="large"
            v-model="form.username"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item class="mt-8" prop="password">
          <el-input
            :prefix-icon="Lock"
            size="large"
            type="password"
            show-password
            v-model="form.password"
            placeholder="密码"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            class="w-100"
            round
            color="#626aef"
            type="primary"
            @click="onSubmit"
            :loading="loading"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref,reactive,onMounted,onBeforeUnmount } from 'vue'
import { toast } from '~/composables/util'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'

const store = useStore()
const router = useRouter()

// do not use same name with ref
const form = reactive({
  username:"",
  password:""
})

const rules = {
    username:[
        { 
            required: true, 
            message: '用户名不能为空', 
            trigger: 'blur' 
        },
    ],
    password:[
        { 
            required: true, 
            message: '用户名不能为空', 
            trigger: 'blur' 
        },
    ]
}

const formRef = ref(null)
const loading = ref(false)
const onSubmit = () => {
    formRef.value.validate((valid)=>{
        if(!valid){
            return false
        }
        loading.value = true
        
        store.dispatch("login",form).then(res=>{
            toast("登录成功")
            router.push("/")
        }).finally(()=>{
            loading.value = false
        })
    })
}

// 监听回车事件
function onKeyUp(e){
    if(e.key == "Enter") onSubmit()
}

// 添加键盘监听
onMounted(()=>{
    document.addEventListener("keyup",onKeyUp)
})
// 移除键盘监听
onBeforeUnmount(()=>{
    document.removeEventListener("keyup",onKeyUp)
})

</script>

<style scoped>
.login-container{
    @apply min-h-screen;
  background-image: url('../assets/login-bg.jpg');
  background-size: cover;
}
.login-container .left,.login-container .right{
    @apply flex items-center justify-center;
}
.login-container .right{
    @apply bg-light-50 flex-col;
}
.left>div>div:first-child{
    @apply font-bold text-5xl text-light-50 mb-4;
}
.left>div>div:last-child{
    @apply text-gray-200 text-sm;
}
.right .title{
    @apply font-bold text-3xl text-gray-800;
}
.right>div{
    @apply flex items-center justify-center my-5 text-gray-300 space-x-2;
}
.right .line{
    @apply h-[1px] w-16 bg-gray-200;
}
</style>